home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
CLOSEMOD
/
CLOSEMOD.C
Wrap
C/C++ Source or Header
|
1991-02-20
|
7KB
|
196 lines
/* Mon, Feb 13, 1989, 2:35:51 PM *******************************************
*******************************************************************************
**
** A sample to demonstrate use of the RAM Serial driver functions
** by a trivial program.
**
** by: Mario P. Vano, VAI
**
** Copyright ⌐1989 by:
**
** Vano Associates, Incorporated
** 2513 Pahl Ave NE
** St. Anthony, MN 55418
** (612)-788-9547
**
** (Creators of The MacChuck(tm) PC Remote Control Program)
**
** Permission is hereby unconditionally granted to anyone to
** use this program fragment for ANY purpose whatsoever!
**
** Note that this code does not represent a realistic way to make
** a terminal program, since it uses a very slow console IO method,
** and does very little error checking. It is only intended to show
** beginning Macintosh COMM programmers how to use the functions.
**
** The Think C project needs to include
**
** 1) SerialIO.C - this source file
** 2) MacTraps
** 3) stdio
**
** The Serial Resource file "SERD" delivered with your compiler needs
** to be present in the same folder as this application or project to run.
** A real application would of course contain the SERD resources in
** its own resource fork (merged by using ResEdit or RMaker.)
**
** Revised Mon, Feb 13, 1989, 2:36:13 PM
** to show how to use SerSta function to read HSKi line.
**
*******************************************************************************
******************************************************************************/
/*|*/
/*| Mike Liveright -- 91/02/20
/*| Slightly Modified because Iall I want to do is to hang up the port,
/*| i.e. CLose it.
/*|*/
#include <Stdio.h>
#include <SerialDvr.H>
/* Assumes the THINK C 3.0 "MacHeaders" include files in use */
main()
{
char rawBuffer[1024]; /* used by the DRIVER - we don't touch it */
char incoming[128]; /* our input buffer */
short inputRef, outputRef; /* the RefNums */
char outChar; /* holds outbound char gotten from keyboard */
SerShk cp; /* Serial Handshake mode setup record */
SerStaRec theStat; /* Status record for reading port status */
long count, i;
OSErr result;
/*
* To simplify debugging, let's try to open a copy of the resource file
* called SERD that is supplied with LSC. If we put a copy of this file
* in the same folder as the project, we can avoid building a resource
* file while we are debugging. A real App would, of course, have the
* drivers found in SERD in its resource file.
*/
result = OpenResFile("\pSERD"); /* ignore any errors here */
inputRef = AinRefNum; /* these are "well-known" refNums */
outputRef = AoutRefNum; /* defined in SerialDrvr.h */
if ((result=RAMSDOpen(sPortA)) != noErr)
;
/*|*/
/*| printf("\nUnable to open modem port!\n"); /*Needed ANSII*/
/*|*/
else
{
/*| printf("\n Terminal IO sample\n"); /*Needed ANSII*/
/*|*/
/*|
/*| |*
/*| * Here's how to configure handshake options if you need to
/*| *|
/*| cp.errs = 0x70; |* build a SerShk record *|
/*| cp.evts = 0;
/*| cp.fCTS = 0; |* Disable CTS handshake for now *|
/*| cp.fDTR = 0; |* Disable DTR handshake for now *|
/*| cp.fInX = 0; |* We don't use input Xon-Xoff *|
/*| cp.fXOn = 0; |* Disable output Xon/Off for now *|
/*| cp.xOn = 0x11; |* Ctrl-Q would be handshake XON *|
/*| cp.xOff = 0x13; |* Ctrl-S would be handshake XOFF *|
/*| SerHShake(outputRef,&cp); |* really should check for errors *|
/*| SerHShake(inputRef, &cp);
/*|
/*| |*
/*| * Here's how to set baud, data bits and stop bits using constants
/*| * defined in SerialDrvr.h
/*| *|
/*| SerReset(outputRef,data8 + stop10 + noParity + baud2400);
/*| SerReset(inputRef, data8 + stop10 + noParity + baud2400);
/*|
/*| |*
/*| * Here's how to increase the driver's private input buffer size.
/*| *|
/*| SerSetBuf(inputRef, rawBuffer, sizeof(rawBuffer));
/*|
/*| |*********************************************************************
/*| * loop here doing terminal emulation until the "Enter" key is hit
/*| * (This is of course a terrible way to do this, but it will do for
/*| * a sample of how to use the drivers).
/*| *********************************************************************|
/*| for (outChar = 0; outChar != 3;) |* 3 == code of "Enter" key *|
/*| {
/*|
/*| SerGetBuf(inputRef,&count); |* any incoming bytes? *|
/*| while (count) |* yes, display them *|
/*| {
/*| if (count > sizeof(incoming)) count = sizeof(incoming);
/*| if ( (FSRead(inputRef, &count, incoming)==noErr)
/*| && count
/*| )
/*| for (i=0; i < count; i++) |* Dumb way to do this! *|
/*| {
/*| incoming[i] &= 0x7f;
/*| if (incoming[i] !='\n')
/*| { |* kludgy unix '\n' fix *|
/*| if (incoming[i]=='\r') incoming[i]='\n';
/*| fputc(incoming[i],stdout);
/*| }
/*| }
/*| SerGetBuf(inputRef,&count); |* Check if any more *|
/*| }
/*|
/*| if (kbhit()) |* Any outbound byte? *|
/*| {
/*| outChar = getch() & 0x7F; |* get from keyboard *|
/*| if (outChar==('@' & 0x1f)) |* cmd @ displays stat *|
/*| {
/*| |*
/*| * This code is entered if you type Command-@.
/*| * It shows how to read the port status from the driver.
/*| * You can read the state of the HSKI handshake line here
/*| * by examining the byte "theStat.ctsHold".
/*| * If your modem is wired as per Apple cable specs, this
/*| * will be wired to its Data Carrier Detect line.
/*| * (Note that your modem configuration RAM or DIP
/*| * switches often override this use, however!)
/*| *|
/*| SerStatus(outputRef,&theStat);
/*| printf("\nResults of SerStatus function call:");
/*| printf("\n cumErrs = %4d xOffSent = %s rdPend = %s",
/*| (short) theStat.cumErrs,
/*| theStat.xOffSent ? "TRUE " : "FALSE",
/*| theStat.rdPend ? "TRUE " : "FALSE"
/*| );
/*| printf("\n wrPend = %s ctsHold = %s xOffHold = %s\n\n",
/*| theStat.wrPend ? "TRUE " : "FALSE",
/*| theStat.ctsHold ? "TRUE " : "FALSE",
/*| theStat.xOffHold ? "TRUE " : "FALSE"
/*| );
/*| }
/*| else
/*| {
/*| if (outChar=='\n') outChar = '\r'; |* do the Unix shuffle *|
/*| count = 1; |* send it to the port *|
/*| FSWrite(outputRef, &count, &outChar);
/*| }
/*| }
/*| }
/*|
/*|
/*| |*
/*| * Here's how to restore the default input buffer for driver.
/*| *
/*| * Warning! VERY BAD THINGS CAN HAPPEN (Perhaps even loss of data
/*| * on your hard disk!) long after your program quits if you forget
/*| * to do this, or forget to close the driver properly!
/*| *
/*| * It is essential that you do not ever leave a custom input buffer
/*| * dangling, EVEN if you abort your program for some other reason!
/*| * The OS will NOT fix this up for you! You have been warned!
/*| *|
/*| SerSetBuf(inputRef,rawBuffer,0); |* how to restore default buffer *|
/*|
/*|*/
/*
* Here's how to close the driver
*/
RAMSDClose(sPortA);
}
}